--
-- Author: shunguo.chen
-- Date: 2018-09-04 17:26:14
--
-- TeamListRender 组队查找器推荐队伍render
--

local NewTeamMemberHeadRender = import(".NewTeamMemberHeadRender")

local ClassRef = app.mvc.createRenderClass(...)

local TEAM_HEIGHT = 100
local SECRET_LAYER_STR = L("lua_code_text_313")

--data = TeamVo
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TeamListNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    local panel = self._ui.Panel
    local initSize = panel:getContentSize()
    self._initSize = initSize
    self:setContentSize(initSize)

    self._nameText = panel:getChildByName("name")
    self._combatText = self._ui.Panel_normal:getChildByName("combat_num")
    self._lvText = self._ui.Panel_normal:getChildByName("lv_num")

    self._layerText = self._ui.Panel_secret:getChildByName("text")
    self._cupText = self._ui.Panel_secret:getChildByName("cup")

    local bgPanel = self._ui.Panel_bg
    local bgs = {}
    for i=1,10 do
        bgs[i] = bgPanel:getChildByName("head" .. i)
    end
    self._bgs = bgs

    local renders = {}
    for i=1,10 do
        local render = NewTeamMemberHeadRender.create(panel:getChildByName("head" .. i))
        render:onClick(handler(self, self._onClickRender))
        renders[i] = render
    end
    self._renders = renders

    self._ui.Button_1:addClickEventListener(function()
        executeFunc(self._onBtnClick, self, "openOtherSet", self._data)
    end)

    self._applyBtn = panel:getChildByName("Button_2")
    self._applyBtn:addClickEventListener(function()
        executeFunc(self._onBtnClick, self, "joinTeam", self._data)
    end)

    self._battleIcon = panel:getChildByName("icon_combat")
    self._battleIcon:addClickEventListener(function()
        executeFunc(self._onBtnClick, self, "joinTeam", self._data)
    end)

    self._appliedBtn = panel:getChildByName("Button_3")
end

function ClassRef:_onClickRender(render)
    local size = render:getPanelSize()
    local pos = render:convertToWorldSpace(cc.p(size.width, size.height/2))
    local info = render:getData()
    executeFunc(self._onBtnClick, self, "teamList_memberOp", pos, info)
end

-- data = NewTeamVo
function ClassRef:refresh()
    if self._data then
        local team = self._data
        self._nameText:setString(team:getLeaderName())

        local teamTargetVo = team:getTeamTargetVo()
        local isSecret = teamTargetVo:isSecret()
        self._ui.Panel_normal:setVisible(not isSecret)
        self._ui.Panel_secret:setVisible(isSecret)
        if isSecret then
            self._layerText:setString(string.format(SECRET_LAYER_STR, team:getLeaderLayer()))
            self._cupText:setString(team:getSecretGradeStr())
        else
            self._combatText:setString(team:getTeamTotalCapacity())
            self._lvText:setString(team:getLvRangeStr())
        end

        local isLegion = team:isLegion()
        local size = isLegion and self._initSize or cc.size(self._initSize.width, TEAM_HEIGHT)
        self:setContentSize(size)
        self._ui.Panel:setContentSize(size)
        ccui.Helper:doLayout(self._ui.Panel)
        for i=6,10 do
            self._bgs[i]:setVisible(isLegion)
            self._renders[i]:setVisible(isLegion)
        end

        local memberMaxNum = team:getMemberMaxNum()
        local members = team:getMembers()
        local robots = team:getRobots()
        for i=1,memberMaxNum do
            local render = self._renders[i]
            local member = members[i]
            if member then
                render:setData(member:getUserBaseInfo())
            else
                local robot = robots[i-#members]
                if robot then
                    render:setData(robot:getBaseInfo())
                else
                    render:setData()
                end
            end
        end

        self:refreshApply()
    end
end

-- 刷新申请状态
function ClassRef:refreshApply()
    local isApplied = self._data:isApplied()
    local isBattle = self._data:isTeamBattle()

    self._appliedBtn:setVisible(isApplied)
    self._battleIcon:setVisible((not isApplied) and isBattle)
    self._applyBtn:setVisible((not isApplied) and (not isBattle))
end

function ClassRef:onBtnClick(func)
    self._onBtnClick = func
end

return ClassRef
